package org.csu.zhangdian.controller;

import org.csu.zhangdian.domain.IMail;
import org.csu.zhangdian.service.IMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;


@Controller
@RequestMapping("/email")
public class IMailController {

    @Autowired
    private IMailService imailService;

    //全体推送页面
    @RequestMapping("/openEveryone")
    public String openEveryone(){
        return "message/sendEmail";
    }

    //全体推送
    @RequestMapping("/sendToEveryone")
    public String sendEmail(String notice){

        List<IMail> a= imailService.getE();
        for(int i=0;i<a.size();i++) {
            imailService.sendSimpleMail(a.get(i).getEmail(), "[ 掌店消息推送 ]",notice);
        }
        return "message/sendToAllSuc";
    }

    //部分推送页面
    @RequestMapping("/openParts")
    public String openParts(){
        return "message/sendEmail2";
    }

    //部分推送
    @PostMapping("sendToParts")
    public String sendToParts(String notice,int lv){
        List<IMail> a= imailService.getE();
        for(int i=0;i<a.size();i++){
            if(a.get(i).getLevel()>=lv){
                imailService.sendSimpleMail(a.get(i).getEmail(),"[ 掌店消息推送 ]",notice);
            }
        }
        return "redirect:/email/openParts";
    }

}
